import tkinter as tk
from tkinter import ttk, messagebox
from tkinter.ttk import Button, Style

from PIL import ImageTk
from PIL.Image import Image

import Login_Window
import pymysql
from ttkbootstrap import Style


class teacher_gui(tk.Tk):
    def __init__(self,current_user):
        super().__init__()
        self.sno = current_user
        self.title("教师窗体")
        self.geometry("600x550+342+123")
        self.resizable(0, 0)
        self.img =tk.PhotoImage(file='用户.png')
        self.setup_UI()
        style = Style(theme='sandstone')
        style.configure('TLabel')
        style.configure('TButton')
        style.configure('TRadiobutton')
        style.configure('TPanedwindow')
        style.configure('Treeview')
        style.configure('TCombobox')
        print(self.sno)


    def setup_UI(self):

        mebubar = tk.Menu(self)
        mebubar.add_command(label="我的课程",command=self.Couse)
        mebubar.add_command(label="补考情况", command=self.Course_Fail)
        mebubar.add_command(label="切换用户", command=self.cancellation)
        self.config(menu=mebubar)

    def Couse(self):
        info = []
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select 课程号 from course where 任教老师 = %s' % (self.sno[0][0])
        cursor.execute(sql)
        result = cursor.fetchall()
        for i in result:
            info.append(i)
        cursor.close()
        conn.close()

        self.value = tk.StringVar()

        self.Pane_center = ttk.PanedWindow(width=600, height=550,style='TPanedwindow')
        ttk.Label(self.Pane_center,image=self.img).place(x=490,y=100)
        ttk.Label(self.Pane_center,text='欢迎你！ '+self.sno[0][1]).place(x=460,y=190)


        ttk.Label(self.Pane_center,text='请选择课程：',style='info.TLabel').place(x=50,y=10)
        com = ttk.Combobox(self.Pane_center, textvariable =self.value,style='info.TCombobox')
        ttk.Button(self.Pane_center, text='修改成绩',style='info.TButton',command=self.update_score_window).place(x=360, y=7,height=30,width=80)
        ttk.Button(self.Pane_center,text='添加成绩',style='info.TButton',command=self.update_score_window1).place(x=460, y=7,height=30,width=80)
        com.bind("<<ComboboxSelected>>", self.chooice)
        com['values'] = info
        self.Pane_center.place(x=0, y=0)
        com.place(x=150,y=10)

        self.Tree = ttk.Treeview(self.Pane_center, columns=("sno","name",
                                                            "score"),
                                 show="headings",style='info.Treeview',
                                 height=20)
        self.Tree.column("sno", width=120, anchor="center")
        self.Tree.column("name", width=120, anchor="center")
        self.Tree.column("score", width=120, anchor="center")

        self.Tree.heading("sno", text="学号")
        self.Tree.heading("name", text="姓名")
        self.Tree.heading("score", text="成绩")
        self.Tree.bind('<ButtonRelease-1>', self.treeviewClick)
        self.Tree.place(x=70, y=70)

    def chooice(self,event):

        for i in self.Tree.get_children():
            self.Tree.delete(i)
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select sc.学号,student.名字,sc.成绩 from sc,student where sc.任教老师 = %s and sc.学号 = student.学号 and ' \
              'sc.课程号 = %s' % (self.sno[0][0],self.value.get())
        cursor.execute(sql)
        result = cursor.fetchall()
        print(result)
        for index in range(len(result)):
            self.Tree.insert("", index, values=(result[index][0], result[index][1],
                                                result[index][2]))
        cursor.close()
        conn.close()

    def chooice_1(self,event):
        for i in self.Tree_1.get_children():
            self.Tree_1.delete(i)
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select 成绩 from sc where 课程号 = %s and 任教老师 = %s and 成绩<60' % (self.value_1.get(),self.sno[0][0])
        cursor.execute(sql)
        result1 = cursor.fetchall()
        if not result1:
            messagebox.showinfo(title='提示',message='没有学生挂科！')
            return

        sql = 'select sc.学号,student.名字,sc.成绩 from sc,student where sc.任教老师 = %s and sc.学号 = student.学号 and sc.成绩<60 ' \
              'and sc.课程号 = %s' % (self.sno[0][0],self.value_1.get())
        cursor.execute(sql)
        result = cursor.fetchall()
        for index in range(len(result)):
            self.Tree_1.insert("", index, values=(result[index][0], result[index][1],
                                                result[index][2]))
        cursor.close()
        conn.close()
    def treeviewClick(self, event):
        for item in self.Tree.selection():
            self.item_text = self.Tree.item(item, "values")
    def treeviewClick_1(self, event):
        for item in self.Tree_1.selection():
            self.item_text_1 = self.Tree_1.item(item, "values")
    def update_score_window(self):

        if self.item_text[2] == 'None':
            messagebox.showerror(title='提示', message='成绩未添加，请先添加成绩！')
            return
        self.update_score = tk.StringVar()
        self.Detail_Window = tk.Toplevel(self.Pane_center)
        self.Detail_Window.geometry('300x160+350+120')
        self.Detail_Window.title('输入成绩')
        ttk.Label(self.Detail_Window,text='新的成绩：',style='TLabel').place(x=60,y=30)
        ttk.Entry(self.Detail_Window, textvariable=self.update_score,style='TEntry').place(x=170,y=30,width=60)
        ttk.Button(self.Detail_Window,text='确定',style='info.TButton',command=self.update_mysql).place(x=100,y=80)


    def update_score_window1(self):
        if self.item_text[2]!='None':
            messagebox.showerror(title='提示',message='成绩已添加！')
            return
        self.credit_1 = tk.IntVar()
        self.update_score = tk.StringVar()
        self.Detail_Window_1 = tk.Toplevel(self.Pane_center)
        self.Detail_Window_1.geometry('300x180+350+120')
        self.Detail_Window_1.title('输入成绩')
        ttk.Label(self.Detail_Window_1, text='新的成绩：', style='TLabel').place(x=60, y=30)
        ttk.Entry(self.Detail_Window_1, textvariable=self.update_score,style='TEntry').place(x=170, y=30, width=60)
        ttk.Label(self.Detail_Window_1, text='学分：', style='TLabel').place(x=60, y=80)
        ttk.Entry(self.Detail_Window_1, textvariable=self.credit_1).place(x=170, y=80, width=110)
        ttk.Button(self.Detail_Window_1, text='确定', style='info.TButton', command=self.update_mysql_0).place(x=100, y=150)

    def update_mysql_0(self):
        self.Detail_Window_1.destroy()
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        cursor.execute("update sc set 成绩='%s',学分='%d' where 学号='%s' and 课程号='%s'" % (self.update_score.get(),self.credit_1.get(),
                                                                             self.item_text[0], self.value.get()))
        conn.commit()
        cursor.close()
        conn.close()
        messagebox.showinfo(title='恭喜', message='修改成功')
    def update_score_window2(self):

        if not self.value_1.get():
            messagebox.showinfo(title='提示', message='请先选择课程！！')
            return
        self.credit = tk.IntVar()
        self.update_score_1 = tk.StringVar()
        self.Detail_Window_2 = tk.Toplevel(self.Pane_center_1)
        self.Detail_Window_2.geometry('300x180+350+120')
        self.Detail_Window_2.title('输入成绩')
        ttk.Label(self.Detail_Window_2, text='成绩：', style='TLabel').place(x=60, y=30)
        ttk.Entry(self.Detail_Window_2, textvariable=self.update_score_1,style='TEntry').place(x=170, y=30, width=60)
        ttk.Label(self.Detail_Window_2, text='学分：', style='TLabel').place(x=60, y=80)
        ttk.Entry(self.Detail_Window_2, textvariable=self.credit,style='TEntry').place(x=170, y=80, width=110)
        ttk.Button(self.Detail_Window_2, text='确定', style='info.TButton', command=self.update_mysql_1).place(x=100, y=150)
    def update_mysql_1(self):
        self.Detail_Window_2.destroy()
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        cursor.execute("update sc set 成绩='%s',学分='%d' where 学号='%s' and 课程号='%s'" % (self.update_score_1.get(),self.credit.get(),
                                                                             self.item_text_1[0], self.value_1.get()))
        conn.commit()
        cursor.close()
        conn.close()
        messagebox.showinfo(title='恭喜', message='添加成功')
    def update_mysql(self):
        self.Detail_Window.destroy()
        print(self.value.get())
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        # sql = "select 课程号 from course where 课程名 = %s" % (self.value.get())
        # cursor.execute(sql)
        # result = cursor.fetchall()
        cursor.execute("update sc set 成绩='%s' where 学号='%s' and 课程号='%s'" % (self.update_score.get(),
                                                                           self.item_text[0],self.value.get()))
        conn.commit()
        cursor.close()
        conn.close()
        messagebox.showinfo(title='恭喜', message='修改成功')

    def Course_Fail(self):
        info = []
        conn = pymysql.connect(host='localhost', user='root', password='123456', database='数据库课程设计')
        cursor = conn.cursor()
        sql = 'select 课程号 from course where 任教老师 = %s' % (self.sno[0][0])
        cursor.execute(sql)
        result = cursor.fetchall()
        for i in result:
            info.append(i)
        cursor.close()
        conn.close()
        self.value_1 = tk.StringVar()
        self.Pane_center_1 = ttk.PanedWindow(width=600, height=550,style='TPanedwindow')
        ttk.Label(self.Pane_center_1, image=self.img).place(x=490, y=100)
        ttk.Label(self.Pane_center_1, text='欢迎你！ ' + self.sno[0][1]).place(x=460, y=190)
        ttk.Label(self.Pane_center_1, text='请选择课程：', style='info.TLabel').place(x=50, y=10)
        comm = ttk.Combobox(self.Pane_center_1, textvariable=self.value_1,style='info.TCombobox')
        ttk.Button(self.Pane_center_1, text='添加补考成绩', style='info.TButton',command=self.update_score_window2).place(x=360, y=7,height=30,width=120)
        comm.bind("<<ComboboxSelected>>", self.chooice_1)
        comm['values'] = info
        self.Pane_center_1.place(x=0, y=0)
        comm.place(x=150, y=10)


        self.Tree_1 = ttk.Treeview(self.Pane_center_1, columns=("sno", "name",
                                                            "score"),
                                 show="headings", style='info.Treeview',
                                 height=20)
        self.Tree_1.column("sno", width=120, anchor="center")
        self.Tree_1.column("name", width=120, anchor="center")
        self.Tree_1.column("score", width=120, anchor="center")

        self.Tree_1.heading("sno", text="学号")
        self.Tree_1.heading("name", text="姓名")
        self.Tree_1.heading("score", text="成绩")
        self.Tree_1.bind('<ButtonRelease-1>', self.treeviewClick_1)
        self.Tree_1.place(x=70, y=70)

    def cancellation(self):
        self.destroy()
        Login_Window.login_gui()